package com.doit.day05

object _02_collect算子 {
  def main(args: Array[String]): Unit = {
    val list: List[Any] = List(1, 1.9, "zss", 18, true, 'a')

    //将里面的数值类型都乘以10返回，其他的类型都不要
    val list1: List[Any] = list.filter(elem => elem.isInstanceOf[Int] || elem.isInstanceOf[Double])
    println(list1.map(elem=>
    if (elem.isInstanceOf[Int]){
      elem.asInstanceOf[Int] * 10
    }else {
      elem.asInstanceOf[Double] * 10
    }
    ))


    //偏函数
    val value: PartialFunction[Any, Double] = new PartialFunction[Any, Double] {
      //类似于filter 判断你要处理的数据  这边如果返回true
      override def isDefinedAt(x: Any) = x.isInstanceOf[Int] || x.isInstanceOf[Double]

      //类似于map
      override def apply(elem: Any) = if (elem.isInstanceOf[Int]) {
        elem.asInstanceOf[Int] * 10
      } else {
        elem.asInstanceOf[Double] * 10
      }
    }

    //用模式匹配
    val doubles: List[Double] = list.collect(value)
    println(doubles)



  }

}
